package com.xcfw.dorm.controller.dormassistant;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.DormAssistan;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.entity.Student;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.seviceimpl.dormassistant.DormManageServiceImpl;
import com.xcfw.dorm.util.DormUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 宿管控制类
 * @author: Bruse Queen
 * @create: 2018-04-16 20:29
 **/
@RestController
@Slf4j
@CrossOrigin
@RequestMapping(value = "/dormAssistant")
public class DormManageController extends BaseController {

    @Autowired
    @Qualifier("dormManageServiceImpl")
    private DormManageServiceImpl dormManageService;

    @RequestMapping(value = "/selectBuildingNum", method = RequestMethod.POST)
    public ReturnJson selectBuildingNum(@RequestBody Student student) {
        try {
            result = dormManageService.selectBuildingNum(student.getBuilding());
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---addAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/getExcelJson", method = RequestMethod.POST)
    public ReturnJson getExcelJson(@RequestBody Student student) {
        try {
            if (student.getBuilding() == null || student.getBuilding().equals("")) {
                throw new ValidationException("必须输入公寓号！");
            }
            result = dormManageService.getExcelJson(student);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---getExcelJson--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/changeDormAssistantPassword", method = RequestMethod.POST)
    public ReturnJson changeDormAssistantPassword(@RequestBody DormAssistan dormAssistan) {
        try {
            if (DormUtil.juedeNull(dormAssistan.getPassword()) || DormUtil.juedeNull(dormAssistan.getIdcard())
                    || DormUtil.juedeNull(String.valueOf(dormAssistan.getSchoolId()))) {
                throw new ValidationException("新密码和身份证不能为空！");
            }
            result = dormManageService.changeDormAssistantPassword(dormAssistan);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---getExcelJson--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }


}
